//
//  CodeButton.m
//  CarCare
//
//  Created by ileo on 14-8-31.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "CodeButton.h"
#import "KVCHelper.h"
#import "UICountingLabel.h"

#define TIME 60

#define NUM_TAG 17

@interface CodeButton()

@property (nonatomic, strong) KVCHelper *kvcEnable;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UICountingLabel *label;

@property (nonatomic, strong) UIView *view;

@property (nonatomic, strong) UIFont *font;

@property (nonatomic, copy) void (^OK)();

@end


@implementation CodeButton

-(void)dealloc{
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    if (self.label) {
        [self.label removeFromSuperview];
        self.label = nil;
    }
}

- (id)initWithFrame:(CGRect)frame action:(void (^)())action ok:(void (^)())ok
{
    self = [super initWithFrame:frame title:LOCSTRING(@"get_code") normalColor:[UIColor clearColor] highlightColor:nil disabledColor:nil action:action];
    [self setTitleColor:COLOR_HEXSTRING(@"#1F90E5") forState:UIControlStateNormal];
    if (self) {
        // Initialization code
        self.font = FONT_DEFAULT_Light(14);
        self.titleLabel.font = self.font;
//        self.titleLabel.textColor = COLOR_HEXSTRING(@"#1F90E5");
        self.layer.cornerRadius = 5;
        __weak __typeof(self) wSelf = self;
        self.kvcEnable = [[KVCHelper alloc] initWithSubject:self forKeyPath:@"enabled" change:^{
            [wSelf enableState];
        }];
        
        self.OK = ok;
    }
    return self;
}

-(void)enableState{
    
    if (self.enabled) {
        
        [self.timer invalidate];
        self.timer = nil;
        [self.label removeFromSuperview];
        self.label = nil;
        [[self viewWithTag:17] removeFromSuperview];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self setTitle:LOCSTRING(@"reget_code") forState:UIControlStateNormal];
        
    }else{
        
        [self setTitle:@"" forState:UIControlStateNormal];
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
        self.titleLabel.textColor = [UIColor grayColor];
        
        UILabel *label = [Factory labelWithFrame:CGRectMake(3, 0, self.width, self.height) font:self.font text:LOCSTRING(@"reget_code") textColor:[UIColor whiteColor] onView:self textAlignment:NSTextAlignmentLeft];
        label.tag = 17;
        self.label = [[UICountingLabel alloc] initWithFrame:CGRectMake(0, 1, 85, self.height)];
        self.label.right = self.width - 3;
        [self addSubview:self.label];
        self.label.font = self.font;
        self.label.textAlignment = NSTextAlignmentLeft;
        self.label.method = UILabelCountingMethodLinear;
        self.label.format = @"重新获取(%d)";
        self.label.tag = NUM_TAG;
        self.label.textColor = [Theme wordGrayNewColor];
        [self.label countFrom:TIME to:1 withDuration:TIME];
        
        
        __weak __typeof(self) wSelf = self;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:TIME block:^{
            wSelf.enabled = YES;
            if (wSelf.OK) {
                wSelf.OK();
            }
        } repeats:NO];
        
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
